package icu.bughub.app.app.ui.components

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import icu.bughub.app.app.viewmodel.MainViewModel
import kotlinx.coroutines.launch
import java.util.Timer
import java.util.TimerTask

/**
 * @author huangzehui
 * @date 2024/5/22 13:37 周三
 * @description 轮播图抽取
 *
 * https://www.bilibili.com/video/BV1aS4y1D7dv?p=10&vd_source=e07cc8cc94eb22bd76722071b03cd199
 */


@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SwiperContent(vm: MainViewModel) {

    // 为了使轮播图可以无限循环，使用虚拟页数
    // 实际页数
    val actualCount = vm.swiperData.size

    // 图片个数是否小于2
    val isLess2 = actualCount < 2

    // 虚拟页数
    val virtualCount = if (isLess2) actualCount else Int.MAX_VALUE

    // 初始图片下标
    val initialIndex = virtualCount / 2


    val pagerState =  rememberPagerState(
        initialPage = initialIndex,
        pageCount = {
            virtualCount
        })

    val coroutineScope = rememberCoroutineScope()

    // 轮播图的自动滚动
    DisposableEffect(key1 = Unit) {
        // 定时器
        val timer = Timer()

        // 开启延迟3秒执行，每3秒执行一次
        timer.schedule(object : TimerTask() {

            override fun run() {
                coroutineScope.launch {
                    pagerState.animateScrollToPage(pagerState.currentPage+1)
                }
            }

        }, 3000, 3000)

        onDispose {
            // 回收的时候，取消掉定时器
            timer.cancel()
        }
    }

// https://developer.android.com/develop/ui/compose/layouts/pager?hl=zh-cn

    HorizontalPager(
        state =pagerState,
//            contentPadding = PaddingValues(horizontal = 16.dp), // contentPadding 和 pageSpacing 结合使用就可以设置页面间的间距
        pageSpacing = if (isLess2) 0.dp else 16.dp,
        modifier = Modifier
            .padding(horizontal = 8.dp)
            .clip(RoundedCornerShape(8.dp))
    ) {
        // https://juejin.cn/post/7134165276760735752

        //        AsyncImage(
//            model = "https://pic.iyingdi.com/post/content/2022/05/14/16325/ed50f6b7-8654-44ff-8762-7596c40928a7.png?imageMogr2/format/jpg|imageMogr2/quality/70",
//            contentDescription = "", modifier = Modifier.width(100.dp).height(30.dp))
//
//
//        Image(
//            painter = rememberAsyncImagePainter("https://pic.iyingdi.com/post/content/2022/05/14/16325/ed50f6b7-8654-44ff-8762-7596c40928a7.png?imageMogr2/format/jpg|imageMogr2/quality/70"),
//            contentDescription = null,
//            modifier = Modifier.size(128.dp)
//        )

//            AsyncImage(model = ImageRequest.Builder(LocalContext.current)
//                .data(vm.swiperData[it].imageUrl)
//                .size(100)
//                .build(), contentDescription = null, modifier = Modifier.width(100.dp).height(40.dp))

        val actualIndex = it % actualCount

        AsyncImage(
            model = vm.swiperData[actualIndex].imageUrl, contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .aspectRatio(7 / 3f)
                .clip(RoundedCornerShape(8.dp)) // contentPadding 和 pageSpacing 的可以使用这个
            ,
            contentScale = ContentScale.Crop
        )
    }
}